package com.sky.business.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sky.business.sys.domain.CameraConfig;
import com.sky.business.sys.domain.Group;
import com.sky.business.sys.domain.Node;
import com.sky.business.sys.param.GroupParam;
import com.sky.business.sys.service.CameraConfigService;
import com.sky.business.sys.service.GroupService;
import com.sky.business.sys.service.NodeService;
import com.sky.common.api.Result;
import com.sky.common.utils.CollectionsUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Tag(name = "设备组管理", description = "设备组控制层")
@RequestMapping("/group")
@Validated
public class GroupController {

    @Autowired
    private  GroupService groupService;

    @Autowired
    private CameraConfigService cameraConfigService;

    @Autowired
    private NodeService nodeService;

    @Operation(summary = "设备组列表")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<Group>> list() {
        List<Group> groupList = groupService.list();
        return Result.success(groupList);
    }

    @Operation(summary = "添加设备组")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> add(@RequestBody GroupParam groupParam) {
        Group group = new Group();
        group.setName(groupParam.getName());
        group.setDescription(groupParam.getDescription());
        groupService.save(group);
        return Result.success();
    }

    @Operation(summary = "修改设备组信息")
    @PostMapping(value = "update", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> update(@RequestBody GroupParam groupParam) {
        LambdaUpdateWrapper<Group> updateWrapper = new LambdaUpdateWrapper<Group>();
        updateWrapper.set(Group::getName, groupParam.getName())
                .set(Group::getDescription, groupParam.getDescription())
                .eq(Group::getId, groupParam.getId());
        groupService.update(updateWrapper);
        return Result.success();
    }

    @Operation(summary = "删除组")
    @GetMapping(value = "del", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> del(@RequestParam("id") @Schema(description = "组ID") Integer id) {
        List<CameraConfig> cameraConfigs = cameraConfigService.list(new LambdaQueryWrapper<CameraConfig>().eq(CameraConfig::getGroupId, id));
        List<Node> nodes = nodeService.list(new LambdaQueryWrapper<Node>().eq(Node::getGroupId, id));
        if(!CollectionsUtil.isNullOrEmpty(cameraConfigs) || !CollectionsUtil.isNullOrEmpty(nodes)){
            return Result.failed("组下存在节点或设备，不允许删除");
        }else{
            groupService.removeById(id);
            return Result.success();
        }
    }




}
